//
//  ViewController.m
//  05-UIScrollView
//
//  Created by 冀永金 on 16/7/20.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
// 宏定义（预编译指令）
// 全大写
#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height

// k + 驼峰命名
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor redColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 300, 40);
    [scrollView addSubview:button];
    button.backgroundColor = [UIColor yellowColor];
    //1. 设置可以滚动的范围 默认 CGSizeZore CGSizeMake(0,0)
    scrollView.contentSize = CGSizeMake(0, 1000);//0 纵向不能滚
    //改变contentOffset什么改变了
    // 改变contentOffset ,实质上是对scrollView的 bouds进项改变
    NSLog(@"offset 前scrollView frame :%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset前 scrollView bouds :%@",NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"offset 后scrollView frame :%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset后 scrollView bouds :%@",NSStringFromCGRect(scrollView.bounds));
    // 3.contentInset
    // 添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(100, 10, 10, 10);
    //锁定垂直或水平滚动（45°角 会失效）
    scrollView.directionalLockEnabled = YES;
    
    //bounces 回弹效果
    scrollView.bounces = YES;
    // 在没有垂直方向的滚动范围内   保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    
    // 在没有水平方向的滚动范围内   保证水平方向的回弹效果存在

    scrollView.alwaysBounceHorizontal = YES;
    // pagingEnabled
    scrollView.pagingEnabled = YES;
    // 滚动指示器
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    //  滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    //indicatorStyle  滚动指示器的颜色
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    
    
    //tracking 用户是否开始拖动
  //  scrollView.tracking
    
   // draggin 用户是否正在拖动
  //  scrollView.dragging
    //decelerating 是否减速中
    //scrollView.decelerating
    //滚动视图 双击状态栏手势是否存在  默认YES
    scrollView.scrollsToTop = YES;
    scrollView.delegate = self;
    
    // 键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    // 测试
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 300, WIDTH - 20, 50)];
    [scrollView addSubview:textField];
    textField.backgroundColor = [UIColor blueColor];
    
    // 缩放系数
    scrollView.maximumZoomScale = 2;
    scrollView.minimumZoomScale = .5;
    
       // Do any additional setup after loading the view, typically from a nib.
}
// 与其想要的子视图绑定
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
// 滚动视图滚动 开始执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.dragging) {
        NSLog(@"正在拖动滚动");
    }else {
    NSLog(@"自己滚动");
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
}
// 已经结束拖拽
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSLog(@"结束拖拽");

}
// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"开始减速");
}
// 已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"结束减速");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
